<?php
namespace wxcode;

class qrcode
{
    // 定义配置项
    public function __construct()
    {
        // 如果是在thinkphp中 那么需要补全/Application/Common/Conf/config.php中的配置
        // 如果不是在thinkphp框架中使用；那么注释掉下面一行代码；直接补全 private $config 即可
        $gzh = config('my')['official_accounts'];
        $this->config['APPID'] = $gzh['app_id'];
        $this->config['APPSECRET'] = $gzh['secret'];
    }
    //二维码带参数
    public function wxappCode($data)
    {
        $wx_token = $this->getAccessToken();
        $post_url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=$wx_token" ;
        $data = json_encode($data);
        //获取二维码
        $result = $this->_requestPost($post_url, $data);

//生成二维码
//        file_put_contents("qrcode.png", $result);//保存成图片;
        $array = json_decode($result,true);
        //换二维码
        $url="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$array['ticket'];
        return $url;

    }
    //客服发送消息

    public function send($data)
    {
        $wx_token = $this->getAccessToken();
        $post_url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=$wx_token" ;
        $data = json_encode($data);
        //发送
        $result = $this->_requestPost($post_url, $data);
//        file_put_contents("qrcode.png", $result);//保存成图片;
        $array = json_decode($result,true);
        //换二维码
        return $array;

    }


    private function getAccessToken()
    {
        // access_token 应该全局存储与更新，以下代码以写入到文件中做示例
        $path = substr(__DIR__,0,-13).'public/';
        $data = json_decode(file_get_contents($path."access_token.json"));
        if ($data->expire_time < time()) {

            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->config['APPID']}&secret=".$this->config['APPSECRET'];
            $res = json_decode($this->httpGet($url));

            $access_token = $res->access_token;
            if ($access_token) {
                $data->expire_time = time() + 7000;
                $data->access_token = $access_token;
                $fp = fopen($path."access_token.json", "w");
                fwrite($fp, json_encode($data));
                fclose($fp);
            }
        } else {
            $access_token = $data->access_token;
        }

        return $access_token;
    }

    private function httpGet($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        $SSL = substr($url, 0, 8) == "https://" ? true : false;
        if ($SSL) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否设置域名
        }
        $content = curl_exec($ch);
        curl_close($ch);
        return $content;
    }

    public function _requestPost($url, $data, $ssl = true)
    {
        //curl完成
        $curl = curl_init();
        //设置curl选项
        curl_setopt($curl, CURLOPT_URL, $url);//URL
        $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0 FirePHP/0.7.4';
        curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);//user_agent，请求代理信息
        curl_setopt($curl, CURLOPT_AUTOREFERER, true);//referer头，请求来源
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);//设置超时时间
        //SSL相关
        if ($ssl) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//禁用后cURL将终止从服务端进行验证
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);//检查服务器SSL证书中是否存在一个公用名(common name)。
        }
        // 处理post相关选项
        curl_setopt($curl, CURLOPT_POST, true);// 是否为POST请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);// 处理请求数据
        // 处理响应结果
        curl_setopt($curl, CURLOPT_HEADER, false);//是否处理响应头
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);//curl_exec()是否返回响应结果

        // 发出请求
        $response = curl_exec($curl);
        if (false === $response) {
            echo '<br>', curl_error($curl), '<br>';
            return false;
        }
        curl_close($curl);
        return $response;
    }
}


?>